<?php
/**
 * Main game engine calls
 *
 * @author Matt Pike
 */
class GameEng extends Controller{

	/**
	 * Load game front end page or redirect if required to lobby/login
	 *
	 * @param $request
	 */
	public static function loadFrontendAction(array $request){
		//if(user is logged in and game exists){
			require_once DirectoryManager::$pathViews."/game-frontend.phtml";
		//}else{
		//	redirect to login/lobby
		//}
	}

	/**
	 * Return all info required to initialise game
	 *
	 * @param array $request
	 * @return string
	 */
	public static function connectToGameAction(array $request){
		require_once(DirectoryManager::$pathModels."/unit/Unit_Base.php");
		//usleep(2000000); // heavy load test

//		/*== NEW INIT CODE READY FOR INCLUSION AFTER MATT MAKES JAVASCRIPT ADJUSTMENTS ==
//		 *
//		 * Marc: ==IMPORTANT==
//		 * This code should obviously only be run by the host otherwise two battle instances will be created!
//		 * As we don't have a lobby yet this will allows us to start the game with one active player and one 'dummy player' to test a few things
//		 *
//		 * This does not take into account any existing battle and just creates a new one with the HARDCODED player ids, 0 & 1
//		 * The Active PlayerId is 0
//		 *
//		 * WARNING - This relies on there already being PlayerIds 0 & 1 in the DB
//		 */
//		//@todo Replace GameEng.php HARDCODED battle init. Should be replaced with dynamic player/battle selection from lobby code (Which hasn't been writen yet!)
//		$newBattleId = Default_Battle::CreateDefault2PlayerBattle(0,1);
//
//		/*
//		 * Marc:
//		 * We need to change how this works a bit,
//		 * likely want to just return a new battle Id from here and query the full battle status afterwards
//		 * so the browser view can be populated
//		 *
//		 * Then we start the polling for updates to the enemy player rows until thier turn has ended
//		 * Then the local player queries their own rows to redraw active units
//		 * In such a way we will essentially push the game onwards from the currently active player always...
//		 *
//		 * @todo Add a battle status field to battle info so inactive player knows if the other player has quit or dropped out
//		 */
//		return json_encode(BattleCommand::GetBattleState_Full($newBattleId));


		// == OLD INIT CODE ==
		$return = array(
			"players" => array(
				array("id"=>1, "name"=>"Matt","team"=>1),
				array("id"=>2, "name"=>"Marc","team"=>2)
			),
			"units" => array(
				// #=id,p=player,r=rotation, t=team (so we can hav different races later), ap = action points (is that the right term for move points?), mt = movement type (how ships behave, perhapps when smaller agile ships get damaged we could change the movement type on them to act like capships)
				array("typ"=>"shuttle1","id"=>"0","p"=>1,"t"=>1,"x"=>4,"y"=>4,"r"=>315,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"1","p"=>1,"t"=>1,"x"=>8,"y"=>6,"r"=>315,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"2","p"=>1,"t"=>1,"x"=>5,"y"=>3,"r"=>0,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"3","p"=>1,"t"=>1,"x"=>6,"y"=>4,"r"=>45,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"4","p"=>1,"t"=>1,"x"=>3,"y"=>2,"r"=>0,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"5","p"=>1,"t"=>1,"x"=>2,"y"=>5,"r"=>0,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"6","p"=>1,"t"=>1,"x"=>6,"y"=>5,"r"=>0,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"7","p"=>1,"t"=>1,"x"=>6,"y"=>3,"r"=>0,"ap"=>2,"mt"=>Config::movementType_agile),

			//	array("typ"=>"corvette1","id"=>"fox4","p"=>1,"t"=>1,"x"=>3,"y"=>5,"r"=>270,"ap"=>2,"mt"=>Config::movementType_heavy),
			//	array("typ"=>"corvette1","id"=>"fox5","p"=>1,"t"=>1,"x"=>7,"y"=>5,"r"=>90,"ap"=>2,"mt"=>Config::movementType_heavy),

				// PLAYER 2
				array("typ"=>"shuttle1","id"=>"8","p"=>2,"t"=>1,"x"=>5,"y"=>1,"r"=>180,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"9","p"=>2,"t"=>1,"x"=>6,"y"=>0,"r"=>180,"ap"=>2,"mt"=>Config::movementType_agile),
				array("typ"=>"shuttle1","id"=>"10","p"=>2,"t"=>1,"x"=>4,"y"=>0,"r"=>180,"ap"=>2,"mt"=>Config::movementType_agile),
				// $ship1->toArray() <- just one way u might want to do it, you would need to write the toArray method tho

				/* MARC: THIS IS THE DB CREATION SCRIPT FOR THE ABOVE UNITS IN BATTLE ID -0-
			 	//PLAYER 1
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[4],$request[4],$request[315]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[8],$request[6],$request[315]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[5],$request[3],$request[0]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[6],$request[4],$request[45]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[3],$request[2],$request[0]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[2],$request[5],$request[0]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[6],$request[5],$request[0]);
				Unit_1T::create1TUnit($request[0],$request[0],$request[0],$request["Y"],$request[6],$request[3],$request[0]);

				//	array("typ"=>"corvette1","id"=>"fox4","p"=>1,"t"=>1,"x"=>3,"y"=>5,"r"=>270,"ap"=>2,"mt"=>Config::movementType_heavy),
				//	array("typ"=>"corvette1","id"=>"fox5","p"=>1,"t"=>1,"x"=>7,"y"=>5,"r"=>90,"ap"=>2,"mt"=>Config::movementType_heavy),

				//PLAYER 2
				Unit_1T::create1TUnit($request[0],$request[1],$request[0],$request["N"],$request[5],$request[1],$request[180]);
				Unit_1T::create1TUnit($request[0],$request[1],$request[0],$request["N"],$request[6],$request[0],$request[180]);
				Unit_1T::create1TUnit($request[0],$request[1],$request[0],$request["N"],$request[4],$request[0],$request[180]);
			 */
			)
		);
		return json_encode($return); // marc: important to always json_encode (Javascript Object Notation) so javascript can interpret values. Actual output would be {"players":[{"name":"Matt","team":1}],"ships":[{"typ":"shuttle1","team":1,"x":0,"y":0},{"typ":"shuttle1","team":1,"x":5,"y":5}]}
	}

	/**
	 * Takes requested unit move, check for validity and returns
	 *
	 * @param array $request
	 * @return string
	 */
	public static function moveUnitAction(array $request){
		//@todo IMPORTANT: Add code to determine unit type which is moving. Suppose this should be part of the $request...
		//HARDCODED - For now we are assuming an Agile Unit_1T movement!
		require_once(DirectoryManager::$pathModels."/unit/Unit_Base.php");
		require_once(DirectoryManager::$pathModels."/unit/Unit_1T.php");
		$result = Unit_1T::move1TUnit($request["unitId"], $request["x"], $request["y"], $request["r"]);
		return json_encode($result);

//		// === OLD CODE =============================
//		//MARC:	THIS WILL ALL BE REMOVED
//		//		MODEL TO TAKE ITS PLACE
//		$battleId = intval($request["battleId"]);
//		$playerId = intval($request["playerId"]);
//		$shipId = intval($request["shipId"]);
//		$newBearing = intval($request["newBearing"]);
//		$newX = intval($request["newX"]);
//		$newY = intval($request["newY"]);
//		$action = stringval($request["action"]);
//
//		// matts example...
//		$unit = new Unit_1T($shipId);
//		$moveSuccess = $unit->move($newX,$newY);
//		if($moveSuccess){
//			return json_encode(array("move-valid"=>1));
//		}else{
//			return json_encode(array("move-valid"=>0,array(
//				"ships"=>array(
//					array("id"=>$shipId,	"x"=>$unit->x, "y"=>$unit->y)
//					// ,array() other ships if req
//				)
//			)));
//		}
//
//		/*
//		if($action = "move"){
//			//Perform Move
//
//		}else{
//			//Invalid input force revert
//			$connection = mysql_connect("localhost",Config::$userName ,Config::$userPassword)
//			OR die(mysql_error());
//		}
//		$return = mysql_query("CALL Move_1(" . mysql_real_escape_string($battleId) . ",
//		" . mysql_real_escape_string($playerId) . ",
//		" . mysql_real_escape_string($shipId) . ",
//		" . mysql_real_escape_string($newBearing) . ",
//		" . mysql_real_escape_string($newX) . ",
//		" . mysql_real_escape_string($newY) . ")");
//		*/
//		//DEBUG LINES
//		echo var_dump($return);
//		//print_r()
//
//		return json_encode($return);
	}

	/**
	 * Polled method to check for general game updates and events
	 *
	 * @param array $request
	 * @return string
	 */
	public static function checkForUpdatesAction(array $request){
			$return = array(
				"moves" => array(
					// #=id,p=player,r=rotation, t=team (so we can hav different races later), ap = action points (is that the right term for move points?), mt = movement type (how ships behave, perhapps when smaller agile ships get damaged we could change the movement type on them to act like capships)
					array("id"=>"8","x"=>5,"y"=>rand(0,2),"r"=>180),
					array("id"=>"9","x"=>7,"y"=>1,"r"=>135),
					array("id"=>"10","x"=>3,"y"=>1,"r"=>225),
					// $ship1->toArray() <- just one way u might want to do it, you would need to write the toArray method tho
				),
				"chat" => array(
					array("p"=>2,"msg"=>htmlentities("hello world ".rand(0,99)))
				)
		);
		// if(NOT players turn){
		// 		$newMoves = Map::checkForMovesFlaggedAsNew(player Id);
		//		foreach($newMoves as $move){
		//			$return[] = array(
		//				"typ" 	=> "move",
		//				"unitId"=> $move->unitId,
		//				"x"		=> $move->x,
		//				"y"		=> $move->y,
		//				"rot"	=> $move->bearing
		//			);
		//		}
		//		@todo add battle events, hacking etc here to occupy player off turn
		// }
		//
		// @todo check for chat updates? 					$return[] = array("typ"=>"chat","msg"=>"fun fun fun")
		// @todo notify if anyone has joined/left game? 	$return[] = array("typ"=>"player-left","pId"=>123)
		// @todo anything else?
		return json_encode($return);
	}

}